package com.zhan.rkforum.service;


import com.zhan.rkforum.bean.Comment;
import com.zhan.rkforum.dao.CommentMapper;
import com.zhan.rkforum.dao.DiscussPostMapper;
import com.zhan.rkforum.util.CommunityConstant;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CommentServiceImpl implements CommentService, CommunityConstant {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private DiscussPostMapper discussPostMapper;


    @Override
    public List<Comment> selectCommentByEntity(int entityType, int entityId, int offset, int limit) {
        return commentMapper.selectCommentByEntity(entityType, entityId, offset, limit);
    }

    @Override
    public int selectCommentCountByEntity(int entityType, int entityId) {
        return commentMapper.selectCommentCountByEntity(entityType, entityId);
    }

    @Override
    public void insertComment(Comment comment) throws IllegalAccessException {
        if(ObjectUtils.isEmpty(comment)){
            throw new IllegalAccessException("评论参数不能为空！");
        }
        int row = commentMapper.insertComment(comment);
        if(comment.getEntityType() == ENTITY_TYPE_POST){
            int count = commentMapper.selectCommentCountByEntity(comment.getEntityType(), comment.getEntityId());
            discussPostMapper.updateCommentCount(comment.getEntityId(), count);
        }
    }

    @Override
    public Comment findCommentById(int commentId) {
        return commentMapper.findCommentById(commentId);
    }
}
